//
//  CNToolBar.swift
//  EmoticonKeyboard
//
//  Created by 陈锦聪 on 16/3/31.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit

enum CNToolBarButtonType: Int {
    case Recent = 0
    case Default
    case Emoji
    case Lxh
}

protocol CNToolBarDelegate: NSObjectProtocol {
    func toolBar(toolBar: CNToolBar, didSelectType type: CNToolBarButtonType)
}

class CNToolBar: UIView {
    // MARK: - 属性
    weak var delegate: CNToolBarDelegate?
    private var previousButton: UIButton?

    class func toolBar() -> CNToolBar {
        return NSBundle.mainBundle().loadNibNamed("CNToolBar", owner: nil, options: nil).last as! CNToolBar
    }
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        scrollToSection(0)
    }
    @IBAction func buttonAction(button: UIButton) {
        didSelectButton(button)
        let type = CNToolBarButtonType(rawValue: button.tag)!
        delegate?.toolBar(self, didSelectType: type)
    }
    private func didSelectButton(button: UIButton) {
        if button == previousButton {
            return
        }
        previousButton?.selected = false
        button.selected = true
        previousButton = button
    }
    func scrollToSection(section: Int) {
        let button = subviews[section] as! UIButton
        didSelectButton(button)
    }
}
